package com.linktrip.devicemanager.control.device;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.Gson;
import com.linktrip.devicemanager.entity.DeviceLog;
import com.linktrip.devicemanager.server.DeviceLogService;
import com.linktrip.devicemanager.util.Constant;
import com.linktrip.devicemanager.util.PageInfo;
import com.linktrip.devicemanager.util.ToolKits;

@Controller
@RequestMapping(value = "/service/device")
public class DeviceLogControl {
	Logger log = LoggerFactory.getLogger(DeviceLogControl.class);
	@Autowired
	private DeviceLogService server;

	@RequestMapping(value = "/listDeviceLog.lt")
	@ResponseBody
	public String listDevicesInfo() {
		List<DeviceLog> list = server.listDeviceLog();
		Gson gson = new Gson();
		return gson.toJson(list);
	}

	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/listDeviceLogPaging.lt")
	@ResponseBody
	public String listDeviceLogPaging(HttpServletRequest request, HttpServletResponse response) {
		
		int pageNo = 1;
		int pageSize = Constant.PAGE_SIZE;
		try {
			pageNo = ToolKits.getRequestParamValueForInt(request, "page_no");
			pageSize = ToolKits.getRequestParamValueForInt(request, "page_size");
		} catch (Exception e) {
			pageSize = Constant.PAGE_SIZE;
			pageNo = 1;
		}
		String device_no = ToolKits.getRequestParamValue(request, "device_no");
		String time1 = ToolKits.getRequestParamValue(request, "time1");
		String time2 = ToolKits.getRequestParamValue(request, "time2");
		long createTime1 = time1 == null ? -1 : ToolKits.date2Long(time1);
		long createTime2 = time2 == null ? -1 : ToolKits.date2Long(time2);
		if (pageNo <= 0) {
			pageNo = 1;
		}

		if (pageSize <= 0) {
			pageSize = Constant.PAGE_SIZE;
		}
		if (ToolKits.isEmpty(device_no)) {
			device_no = null;
		}
		
		int start = (pageNo - 1) * pageSize;
		int end = pageSize;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("device_no", device_no);
		map.put("createTime1", createTime1);
		map.put("createTime2", createTime2);
		map.put("start", start);
		map.put("end", end);
		log.info("start:{} end:{}",start,end);
		List<DeviceLog> list = server.listDeviceLogPaging(map);
		PageInfo<List> plist = new PageInfo<List>(server.totals(map), list);
		plist.setPageNo(pageNo);
		plist.setPageSize(pageSize);
		plist.setPagesSize(plist.pagesSize());
		
		Gson gson = new Gson();
		return gson.toJson(plist);
	}

	@RequestMapping(value = "/listDeviceLogByNo.lt")
	@ResponseBody
	public String listDeviceLogByNo(HttpServletRequest request, HttpServletResponse response) {
		String device_no = ToolKits.getRequestParamValue(request, "device_no").trim();
		System.out.println(device_no);
		List<DeviceLog> list = server.listDeviceLogByNo(device_no);
		Gson gson = new Gson();
		return gson.toJson(list);
	}

	@RequestMapping(value = "/removeDeviceLog.lt")
	@ResponseBody
	public String removeDevicesInfo(HttpServletRequest request, HttpServletResponse response) {
		/*
		 * boolean result = false; String device_no =
		 * ToolKits.getRequestParamValue(request, "device_no"); DeviceInfo
		 * entity = server.getDeviceInfoByDeviceNo(device_no); if (entity !=
		 * null) result = server.removeDeviceInfo(entity);
		 * 
		 * int res = result ? Constant.RESPONSE_OK : Constant.RESPONSE_ERR; Gson
		 * gson = new Gson(); return gson.toJson(res);
		 */
		return null;
	}

	@RequestMapping(value = "/showDeviceLogDetails.lt")
	@ResponseBody
	public String showDevicesInfoDetails(HttpServletRequest request, HttpServletResponse response) {
		/*
		 * String device_no = ToolKits.getRequestParamValue(request,
		 * "device_no"); DeviceInfo entity =
		 * server.getDeviceInfoByDeviceNo(device_no); Gson gson = new Gson(); if
		 * (entity != null) return gson.toJson(entity);
		 * 
		 * int res = Constant.RESPONSE_ERR;
		 * 
		 * return gson.toJson(res);
		 */
		return null;
	}
}
